package com.ggh.jinjilive.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cy.dialog.BaseDialog;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.dialog.adapter.SelectGoodsAdapter;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectGoodsDialog extends BaseDialog implements OnRefreshListener, OnLoadMoreListener{
    //刷新控件
    private SmartRefreshLayout refreshLayout;
    //展示控件
    private RecyclerView recyclerView;
    //适配器
    private SelectGoodsAdapter selectGoodsAdapter;
    //刷新还是加载
    public boolean isRefresh;
    //页数
    public int page = 1;
    //
    private LoadService loadService;

    public SelectGoodsDialog(Context context) {
        super(context);
    }

    public SelectGoodsDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView(R.layout.dialog_select_goods);
        animType(AnimInType.BOTTOM);
        //刷新控件
        refreshLayout = findViewById(R.id.refreshLayout);
        //刷新
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        //刷新
        refreshLayout.setOnRefreshListener(this);
        //加载
        refreshLayout.setOnLoadMoreListener(this);
        onRefresh(refreshLayout);
        //展示控件
        recyclerView = findViewById(R.id.recyclerView);
        selectGoodsAdapter = new SelectGoodsAdapter();
        recyclerView.setAdapter(selectGoodsAdapter);

        loadService = LoadSir.getDefault().register(refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                onRefresh(refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        onRefresh(refreshLayout);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        page = 1;
        Map<String, Object> map = new HashMap<>();
        map.put("page" ,page);
        map.put("status" ,"1");
        GeneralUtils.shopProductList(map, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                refreshLayout.finishRefresh();
                List<MallGoods> mallGoodsList = (List<MallGoods>) mObject;
                if(null != mallGoodsList && mallGoodsList.size() > 0){
                    loadService.showSuccess();
                    selectGoodsAdapter.setAdd(mallGoodsList);
                }else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        page = page + 1;
        Map<String, Object> map = new HashMap<>();
        map.put("page" ,page);
        map.put("status" ,"1");
        GeneralUtils.shopProductList(map, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                List<MallGoods> mallGoodsList = (List<MallGoods>) mObject;
                if(null != mallGoodsList && mallGoodsList.size() > 0){
                    selectGoodsAdapter.setAdd(mallGoodsList);
                    refreshLayout.finishLoadMore();
                }else {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        });
    }



    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
